dojo.provide("lotex.system.Loader");

dojo.require("lotex.system.Uri");
dojo.require("dojo.io.*");
dojo.require("dojo.event.*");

lotex.system.Loader = {
    description: "",
    add: function(desc,obj,func,complete) {
        this.description = desc;
        dojo.event.connect(dojo, "loaded", obj,func);
        dojo.event.kwConnect({
            srcObj: obj,
            srcFunc: func,
            adviceObj:  this,
            adviceFunc: "beforeLoad",
            adviceType: "before"
        });
        dojo.event.kwConnect({
            srcObj: obj,
            srcFunc: complete,
            adviceObj:  this,
            adviceFunc: "afterLoad",
            adviceType: "after"
        });
    },

    afterLoad: function() {
        dojo.debug(this.description + " load complete!");
    },

    beforeLoad: function() {
        dojo.debug(this.description + " loading...");
    },

    loadJSON: function (uri) {
        uri = lotex.system.Uri(uri);
        var bindArgs = {
            url: uri,
            error: function(type, data, evt){
                dojo.debug("Read uri error! ( uri:[" + uri + "] )");
            },
            mimetype: "text/json"
        };
        var req = dojo.io.bind(bindArgs);
        return req;
    },

    loadTemplate: function (uri) {
        uri = lotex.system.Uri(uri);
        var bindArgs = {
            url: uri,
            error: function(type, data, evt){
                dojo.debug("Read uri error! ( uri:[" + uri + "] )");
            },
            mimetype: "text/plain"
        };
        var req = dojo.io.bind(bindArgs);
        return req;
    },

    loadCommand: function (uri) {
        uri = lotex.system.Uri(uri);
        var bindArgs = {
            url: uri,
            error: function(type, data, evt){
                dojo.debug("Read uri error! ( uri:[" + uri + "] )");
            },
            mimetype: "text/javascript"
        };
        var req = dojo.io.bind(bindArgs);
        return req;
    }
}